package br.ufrn.sigtcc.model.utils;



import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.ufrn.sigtcc.data.DAOProfessorImpl;
import br.ufrn.sigtcc.data.interfaces.IDAOProfessor;
import br.ufrn.sigtcc.model.entidades.Professor;
import br.ufrn.sigtcc.util.exceptions.BDException;


public class ProfessorConverter implements Converter {

	static IDAOProfessor dao = new DAOProfessorImpl();
	
    public static List<Professor> professorDB;
   
    static {
        professorDB = new ArrayList<Professor>();
        try {
			professorDB = dao.listarProfessores();
		} catch (BDException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }

    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
     
    	if (submittedValue.trim().equals("")) {
            return null;
        } else {
                for (Professor p : professorDB) {
            	
                    if (p.getNome().equalsIgnoreCase(submittedValue)) {
                    		
                        	return p;
                    }
              
                }
       }
        return null;
    }

    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
    	
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Professor) value).getNome());
        }
    }
}
                    